using System;
using System.ComponentModel.DataAnnotations;

namespace MalwareMultiScan.Api.Attributes
{
    /// <summary>
    /// Validate URI to be an absolute http(s) URL.
    /// </summary>
    public class IsHttpUrlAttribute : ValidationAttribute
    {
        /// <inheritdoc />
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (!Uri.TryCreate((string) value, UriKind.Absolute, out var uri)
                || !uri.IsAbsoluteUri
                || uri.Scheme.ToLowerInvariant() != "http"
                && uri.Scheme.ToLowerInvariant() != "https")
                return new ValidationResult("Only absolute http(s) URLs are supported");

            return ValidationResult.Success;
        }
    }
}